package com.jeeplus.web.controller;

import com.jeeplus.web.entities.system.ScheduleJobEntity;
import com.jeeplus.web.service.system.ScheduleJobService;
import com.jeeplus.web.util.PageUtils;
import com.jeeplus.web.common.R;
import com.jeeplus.web.common.exception.RRException;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:yuzp17311
 * @version:v1.0
 * @date: 2016-12-12 14:35.
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    /**

     * 定时任务列表

     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:schedule:list")
    public R list(String beanName,Integer page, Integer limit){
        Map<String, Object> map = new HashMap<>();
        map.put("beanName", beanName);
        map.put("offset", (page - 1) * limit);
        map.put("limit", limit);

        //查询列表数据


        List<ScheduleJobEntity> jobList = scheduleJobService.queryList(map);
        int total = scheduleJobService.queryTotal(map);

        PageUtils pageUtil = new PageUtils(jobList, total, limit, page);

        return R.ok().put("page", pageUtil);
    }

    /**

     * 定时任务信息

     */
    @RequestMapping("/info/{jobId}")
    @RequiresPermissions("sys:schedule:info")
    public R info(@PathVariable("jobId") Long jobId){
        ScheduleJobEntity schedule = scheduleJobService.queryObject(jobId);

        return R.ok().put("schedule", schedule);
    }

    /**

     * 保存定时任务

     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:schedule:save")
    public R save(@RequestBody ScheduleJobEntity scheduleJob){
        //数据校验


        verifyForm(scheduleJob);

        scheduleJobService.save(scheduleJob);

        return R.ok();
    }

    /**

     * 修改定时任务

     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:schedule:update")
    public R update(@RequestBody ScheduleJobEntity scheduleJob){
        //数据校验


        verifyForm(scheduleJob);

        scheduleJobService.update(scheduleJob);

        return R.ok();
    }

    /**

     * 删除定时任务

     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:schedule:delete")
    public R delete(@RequestBody Long[] jobIds){
        scheduleJobService.deleteBatch(jobIds);

        return R.ok();
    }

    /**

     * 立即执行任务

     */
    @RequestMapping("/run")
    @RequiresPermissions("sys:schedule:run")
    public R run(@RequestBody Long[] jobIds){
        scheduleJobService.run(jobIds);

        return R.ok();
    }

    /**

     * 暂停定时任务

     */
    @RequestMapping("/pause")
    @RequiresPermissions("sys:schedule:pause")
    public R pause(@RequestBody Long[] jobIds){
        scheduleJobService.pause(jobIds);

        return R.ok();
    }

    /**

     * 恢复定时任务

     */
    @RequestMapping("/resume")
    @RequiresPermissions("sys:schedule:resume")
    public R resume(@RequestBody Long[] jobIds){
        scheduleJobService.resume(jobIds);

        return R.ok();
    }

    /**

     * 验证参数是否正确

     */
    private void verifyForm(ScheduleJobEntity scheduleJob){
        if(StringUtils.isBlank(scheduleJob.getBeanName())){
            throw new RRException("bean名称不能为空");
        }

        if(StringUtils.isBlank(scheduleJob.getMethodName())){
            throw new RRException("方法名称不能为空");
        }

        if(StringUtils.isBlank(scheduleJob.getCronExpression())){
            throw new RRException("cron表达式不能为空");
        }
    }

}
